package cn.com.surker.database;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Author lucky
 * @create 2021年03月18 16:00
 * @desc
 */
@Configuration
public class DataSourceConfig {

	/**
	 * 主数据源
	 **/
    @Primary
    @Bean(name = "mysqlDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.mysql")
    public DataSourceProperties mysqlDataSourceProperties() {
        return new DataSourceProperties();
	}

	/**主数据源**/
    @Primary
    @Bean(name = "mysqlDataSource")
    public DataSource mysqlDataSource(@Qualifier("mysqlDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
	}

	/**SqlServer数据源**/
    @Bean(name = "sqlServerDataSource")
    @Qualifier("sqlServerDataSource")
    @ConfigurationProperties(prefix="spring.datasource.template")
    public DataSource sqlServerDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="sqlServerJdbcTemplate")
    public JdbcTemplate sqlServerJdbcTemplate(@Qualifier("sqlServerDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
	}

	/**OAMysql数据源**/
    @Bean(name = "oaMysqlDataSource")
    @Qualifier("oaMysqlDataSource")
    @ConfigurationProperties(prefix="spring.datasource.oa.template")
    public DataSource oaMysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="oaMysqlJdbcTemplate")
    public JdbcTemplate oaMysqlJdbcTemplate(@Qualifier("oaMysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

	@Bean(name="mysqlJdbcTemplate")
	public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}
}
